package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.SysJobLog;
import com.ccp.dev.system.service.SysJobLogService;
import com.ccp.dev.core.basic.annotion.Action;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 后台任务执行日志 信息操作处理控制器
 *
 * @author hw
 * @date 2019-08-08 14:16:10
 */
@Controller
@RequestMapping("/system/sysJobLog")
@Action(ownermodel = SysAuditModelType.SYSTEM_SETTING)
public class SysJobLogController extends SysBaseController{
    @Resource
    private SysJobLogService sysJobLogService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: hw
     * @date: 2019-08-08 14:16:10
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="后台任务执行日志首页页面跳转",exectype = "页面跳转日志")
    public String list(HttpServletRequest request,ModelMap modelMap) {
        String jobName = RequestUtil.getString(request,"jobName");
        String baseUrl = RequestUtil.getString(request,"baseUrl");
        modelMap.put("jobName",jobName);
        modelMap.put("baseUrl",baseUrl);
        return "/scheduler/jobLogList";
    }

    /**
     * 功能描述: 根据主键ID删除数据的通用删除方法
     *
     * @param: id 参数名称必须是"logId"
     * @return: java.lang.Object
     * @auther: hw
     * @date: 2019-08-08 14:16:10
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="根据主键ID删除后台任务执行日志数据",
            detail = "根据主键ID删除后台任务执行日志数据<#if success>成功<#else>失败</#if>",
            exectype = "删除日志")
    public Object delete(@RequestParam("id") String logId) {
        if (StringUtil.isEmpty(logId)) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("delete.failure.message");
        }
        try{
            sysJobLogService.delByIdArray(logId.split(","));
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return  getResultDataSuccess("delete.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("delete.failure");
        }
    }

    /**
     * 功能描述: 附带检索条件的分页查询
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: hw
     * @date: 2019-08-08 14:16:10
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description="附带检索条件的分页查询后台任务执行日志",exectype = "查询日志")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        queryFilter.put("jobName",RequestUtil.getString(request,"jobName"));
        //分页数据
        List<SysJobLog> pageDatas = sysJobLogService.queryJobLogListByJobName(queryFilter);
        return success(pageDatas, queryFilter.getPageBean().getTotalCount());
    }

}
